package com.weme.home

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.resources.R
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.purepulse.home.databinding.FragmentHomeBinding
import com.purepulse.home.databinding.ItemHomeHotBinding
import com.weme.common.arouter.ARouterHelper
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseFragment
import com.weme.common.manager.SocketConnectManager.accountLiveData
import com.weme.common.util.UserMKHelper
import com.weme.common.util.setOnFastClickListener
import com.weme.home.fragment.HomeHotFragment
import com.weme.home.fragment.HomeNewFragment
import com.weme.home.viewmode.HomeViewMode
import dev.kratos.api.user_server.v1.GetAccountInfoReply


class VH(
    parent: ViewGroup,
    val binding: ItemHomeHotBinding = ItemHomeHotBinding.inflate(LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root)

@Route(path = ARouterPaths.HOMEFRAGMENT)
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewMode>() {
    var homeHotFragment :HomeHotFragment?=null

    override fun startOperate() {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.calView).init()
        homeHotFragment = HomeHotFragment()
        //  mBinding.tvBalance.text = _customAccount?.coins.toString()
        val fragments = listOf(homeHotFragment, HomeNewFragment())
        val labels = listOf(getString(com.example.resources.R.string.home_hot), getString(com.example.resources.R.string.home_new))

        mBinding.vp2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(p0: Int): Fragment {
                return fragments[p0]!!
            }
        }

        mBinding.indicator.attachViewPager(mBinding.vp2, labels)
        accountLiveData.observe(this) {
            if (TextUtils.isEmpty(it.coins.toString())) {
                mBinding.tvBalance.text = "0"
            } else {
                mBinding.tvBalance.text = it.coins.toString()
            }
        }

        try {
            val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
            recyclerViewField.isAccessible = true
            val recyclerView = recyclerViewField.get(mBinding.vp2) as RecyclerView
            val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
            touchSlopField.isAccessible = true
            val touchSlop = touchSlopField.get(recyclerView) as Int
            touchSlopField.set(recyclerView, touchSlop * 4) // 调整触摸敏感度（6 是经验值）
        } catch (ignore: Exception) { // 处理异常
        }

    }
    fun onRefresh(){
        mBinding.vp2.setCurrentItem(0,false)

    }

    fun initAccount(it: GetAccountInfoReply) {
      //  mBinding.tvVip.text = if (it.vipLevel == 0) resources.getString(com.example.resources.R.string.match_become_vip) else resources.getString(com.example.resources.R.string.match_vip)
        UserMKHelper.encode(UserMKHelper.VIP_LEVEL, it.vipLevel)
        mBinding.tvBalance.post {
            if (TextUtils.isEmpty(it.account.coins.toString())) {
                mBinding.tvBalance.text = "0"
            } else {
                mBinding.tvBalance.text = it.account.coins.toString()
            }
        }


//        mBinding.scvBalance.setVisible(it.vipLevel > 0)
//        mBinding.imBalance.setVisible(it.vipLevel > 0)
    }



    override fun clickListener() {
        mBinding.imBalance.setOnClickListener {
            ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init() }
        mBinding.imBalance.setOnFastClickListener {
            ARouterHelper.navigateToActivity(requireContext(), ARouterPaths.MINE_COINS_ACTIVITY)
        }
        mBinding.scvBalance.setOnFastClickListener {
            ARouterHelper.navigateToActivity(requireContext(), ARouterPaths.MINE_COINS_ACTIVITY)
        }

    }

    override fun getViewModel(): Class<HomeViewMode> = HomeViewMode::class.java
}